/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package store.forms;

/**
 *
 * @author Irvin
 */
public class Register extends javax.swing.JPanel {

    /**
     * Creates new form Register
     */
    public Register() {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        usernameLabel = new javax.swing.JLabel();
        passwordLabel = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        username = new javax.swing.JTextField();
        password = new javax.swing.JPasswordField();
        error = new javax.swing.JLabel();
        confirm = new javax.swing.JPasswordField();
        ConfirmLabel = new javax.swing.JLabel();
        nameLabel = new javax.swing.JLabel();
        fullname = new javax.swing.JTextField();
        storeLabel = new javax.swing.JLabel();
        store = new javax.swing.JComboBox();
        for (store.Store s: storeList()) {
            store.addItem(s.getId() + " - " + s.getName());
        }
        RegisterLabel = new javax.swing.JLabel();

        usernameLabel.setText("Username");

        passwordLabel.setText("Password");

        error.setForeground(new java.awt.Color(255, 51, 51));
        error.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);

        ConfirmLabel.setText("Confirm");

        nameLabel.setText("Name");

        storeLabel.setText("Store");

        RegisterLabel.setFont(new java.awt.Font("Times New Roman", 0, 18)); // NOI18N
        RegisterLabel.setText("Register");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(error, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                    .addComponent(passwordLabel)
                                    .addGap(18, 18, 18))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(usernameLabel)
                                    .addGap(16, 16, 16)))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(ConfirmLabel)
                                    .addComponent(storeLabel)
                                    .addComponent(nameLabel))
                                .addGap(27, 27, 27)))
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(fullname)
                            .addComponent(confirm)
                            .addComponent(password)
                            .addComponent(username)
                            .addComponent(store, 0, 267, Short.MAX_VALUE)))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(RegisterLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 60, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(7, 7, 7)
                .addComponent(RegisterLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(nameLabel)
                    .addComponent(fullname, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usernameLabel)
                    .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(passwordLabel)
                    .addComponent(password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(ConfirmLabel)
                    .addComponent(confirm, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(storeLabel)
                    .addComponent(store, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(error, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    public String registerUser() {
        String pass = new String(password.getPassword());
        String conf = new String(confirm.getPassword());
        
        if (pass.equals(conf)) {
            String user = username.getText();
            String name = fullname.getText();
            int id = Integer.parseInt(
                    store.getSelectedItem().toString().split(" - ")[0]);
            return register(user, pass, name, id);
        } else {
            error.setText("Password do not match.");
            return "401:Password do not match.";
        }
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel ConfirmLabel;
    private javax.swing.JLabel RegisterLabel;
    private javax.swing.JPasswordField confirm;
    private javax.swing.JLabel error;
    private javax.swing.JTextField fullname;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel nameLabel;
    private javax.swing.JPasswordField password;
    private javax.swing.JLabel passwordLabel;
    private javax.swing.JComboBox store;
    private javax.swing.JLabel storeLabel;
    private javax.swing.JTextField username;
    private javax.swing.JLabel usernameLabel;
    // End of variables declaration//GEN-END:variables

    private static java.util.List<store.Store> storeList() {
        store.StoreWebService_Service service = 
                new store.StoreWebService_Service();
        store.StoreWebService port = service.getStoreWebServicePort();
        return port.storeList();
    }

    private static String register(java.lang.String username,
            java.lang.String password, java.lang.String fullname, int storeid) {
        store.StoreWebService_Service service = 
                new store.StoreWebService_Service();
        store.StoreWebService port = service.getStoreWebServicePort();
        return port.register(username, password, fullname, storeid);
    }

    /*public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(() -> {
            javax.swing.JFrame window = new javax.swing.JFrame();
            window.setSize(400, 400);
            window.setLayout(new java.awt.BorderLayout());
            Register panel = new Register();
            window.add(panel, java.awt.BorderLayout.CENTER);
            javax.swing.JButton action = new javax.swing.JButton("ACTION");
            action.addActionListener((e) -> {
                System.out.println(panel.registerUser());
            });
            window.add(action, java.awt.BorderLayout.SOUTH);
            window.setLocationRelativeTo(null);
            window.setVisible(true);
            window.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
        });
    }*/
}
